//
//  UpdateAccessOperation.m
//  UCiEnabler
//
//  Created by Delio Brignoli on 26/07/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "UpdateAccessOperation.h"
#import "Reachability.h"
#import "IEnablerComms.h"
#include <unistd.h>

#define UCWEB_HOSTNAME "www.canterbury.ac.nz"
#define IENABLER_HOSTNAME "ienabler.canterbury.ac.nz"
#define GOOGLE_HOSTNAME "www.google.com"

@implementation UpdateAccessOperation

@synthesize currentStatus;
@synthesize targetStatus;
@synthesize username;
@synthesize password;
@synthesize hint;
@synthesize reason;


- (id)initWithStatus:(IEnablerStatus)_targetStatus for:(NSString *)usr with:(NSString *)pwd {
	if (self = [super init]) {
		// Initialization code
		self.username = usr;
		self.password = pwd;
		self.targetStatus = _targetStatus;
		[self reset];
	}
	return self;	
}

- (void)reset {
	currentStatus = IEnablerServiceStatusUnknown;
}

- (void)checkReachability {

	//Are we connected?
	NetworkStatus ns = [[Reachability sharedReachability] internetConnectionStatus];
	if (ns == NotReachable) {
		//Set success flag to false, set reason and exit
		currentStatus = IEnablerServiceNotReachable;
		reason = @"Network connection unavailable";
		hint = @"Connect to the UC WiFi network and try again.";
		return;
	}
	//Are we connected via WiFi?
	if (ns == ReachableViaCarrierDataNetwork) {
		//Set success flag to false, set reason and exit
		currentStatus = IEnablerServiceNotReachable;
		reason = @"Connected via carrier network";
		hint = @"Connect to the UC WiFi network and try again.";
		return;
	}
	
	[[Reachability sharedReachability] setHostName:@UCWEB_HOSTNAME];
	ns = [[Reachability sharedReachability] remoteHostStatus];
	if (ns == NotReachable) {
		//Set success flag to false, set reason and exit
		currentStatus = IEnablerServiceNotReachable;
		reason = @"Network connection unavailable";
		hint = @"Connect to the UC WiFi network and try again.";
		return;
	}
	//Are we connected via WiFi?
	if (ns == ReachableViaCarrierDataNetwork) {
		//Set success flag to false, set reason and exit
		currentStatus = IEnablerServiceNotReachable;
		reason = @"Connected via carrier network";
		hint = @"Connect to the UC WiFi network and try again.";
		return;
	}	
	
	//Are we connected to the UC network?
	[[Reachability sharedReachability] setHostName:@IENABLER_HOSTNAME];
	ns = [[Reachability sharedReachability] remoteHostStatus];
	if (ns != ReachableViaWiFiNetwork) {
		//Set success flag to false, set reason and exit
		currentStatus = IEnablerServiceNotReachable;
		reason = @"Not connected to UC WiFi network";
		hint = @"Connect to the UC WiFi network and try again.";
		return;
	}

	currentStatus = IEnablerServiceReachable;
}

- (void)deduceAccessState {
	
	NSError *error;
	NSURLResponse *response;
	[NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:
								[NSURL URLWithString:@"http://" GOOGLE_HOSTNAME]
								cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
								timeoutInterval:3.0]
						  returningResponse:&response
						  error:&error];

	if (error && error.code != 0)
		currentStatus = IEnablerServiceAccessDisabled;
	else
		currentStatus = IEnablerServiceAccessEnabled;	
}

- (void)main {
	[self reset];

	//sleep(1);

	[self checkReachability];

	if (currentStatus == IEnablerServiceReachable) {
	
		int state = [IEnablerComms updateAccessTo:(targetStatus == IEnablerServiceAccessEnabled) forUser:username withPwd:password];
		if (state < 4) {
			currentStatus = IEnablerServiceError;
			reason = @"Invalid username or password.";
			hint = @"Enter your correct username and password then try again.";
			return;
		}
		
		sleep(1);
		
		[self deduceAccessState];

	}
}

@end

@implementation DeduceAccessOperation

- (id)init {
	if (self = [super init]) {
		// Initialization code
		[self reset];
	}
	return self;	
}

- (void)main {
	[self reset];
	
	[self checkReachability];
	if (currentStatus == IEnablerServiceReachable)
		[self deduceAccessState];
}

@end
